<?php
/**
 * get SysInfo
 */
if(!defined('THINK_PATH')) exit;
class SystemInfoAction extends Action {
	private $gd;
	private $serverEnv;
	private $domainName;
	private $phpVersion;
	private $gdInfo;
	private $freeType;
	private $mysqlVersion;
	private $allowUrlFopen;
	private $curlInfo;
	private $fileUpload;
	private $dbSize;
	private $maxExeTime;
	
	public function __construct() {
		parent::__construct();
	
		$this->serverEnv =$this->getServerEnv();
		$this->domainName = $this->getDomainName();
		$this->phpVersion = $this->getPhpVersion();
		$this->gdInfo = $this->getGdInfo();
		$this->freeType = $this->getFreeType();
		$this->mysqlVersion = $this->getMysqlVersion();
		$this->allowUrlFopen = $this->getAllowUrlFopen();
		$this->curlInfo = $this->getCurlVersion();
		$this->fileUpload = $this->getFileUpload();
		$this->dbSize = $this->getDbSize();
		$this->maxExeTime = $this->getMaxExeTime();
		
		
	}
	
	/**
	 * get the environment of server
	 */
	private function getServerEnv() {
		return PHP_OS.' | '.$_SERVER['SERVER_SOFTWARE'];
	}
	
	/**
	 * get domainName
	 */
	private function getDomainName() {
		return $_SERVER['SERVER_NAME'];
	}
	
	/**
	 * get the version of php
	 */
	private function getPhpVersion() {
		return PHP_VERSION;
	}

	/**
	 * support gd or not
	 */
	private function getGdInfo() {
		$gdInfo = '';
		if(function_exists('gd_info')){
			$this->gd = gd_info();
			$gdInfo = $this->gd['GD Version'];
		} else {
			$gdInfo = '<span style="color:red">未知</span>';
		}
		return $gdInfo;
	}

	/**
	 * support freeType or not
	 */
	private function getFreeType() {
		if($this->gd["FreeType Support"])
			return '支持';
		return '<span style="color:red">不支持</span>';
	}

	/**
	 * get the version of mysql
	 */
	private function getMysqlVersion() {
		$model = M();
		$rs = $model->query('select version() as v');
		unset($model);
		return $rs[0]['v'];
	}

	private function getAllowUrlFopen() {
		if(@ini_get('allow_url_fopen'))
			return '支持';
		return '<span style="color:red">不支持</span>';
	}
	
	private function getCurlVersion() {
		if (extension_loaded('curl')) {
			$arr = curl_version();
			return $arr['version'] . ' | ' . $arr['ssl_version'] . ' | host: ' . $arr['host'];
		}
		return '<span style="color:red">不支持</span>';
	}

	/**
	 * get the size of upload and form 
	 */
	private function getFileUpload() {
		if(@ini_get('file_uploads')) {
			$upFileSize = ini_get('upload_max_filesize');
			$postFileSize = ini_get('post_max_size');
			return '允许 -- 文件: '.$upFileSize.' | 表单: '.$postFileSize;
		} 
		return '<span style="color:red">禁止上传</span>';
	}
	
	/**
	 * get used size of database
	 */
	private function getDbSize() {
		$sql = 'SHOW TABLE STATUS FROM ' . C('DB_NAME');
		$tblPrefix = C('DB_PREFIX');
		if( !empty($tblPrefix) ) {
			$sql .= " LIKE '$tblPrefix%'";
		}
		$model = M();
		$result = $model->query($sql);
		unset($model);
		$size = 0;
		for($i=0; $i<count($result); $i++) {
			for($j=0; $j<count($result[$i]); $j++) {
				$size += $result[$i]['Data_length'] + $result[$i]['Index_length'];
			}
		}
		
		return $this->getSize($size);
	}

	/**
	 * get the max execute time of script
	 */
	private function getMaxExeTime() {
		return ini_get('max_execution_time') . ' 秒';
	}
	
	private function getSize($size) {
		$unit = 'Bytes';
		if($size < 1024) {
			return $size.$unit;
		} else if($size < pow(1024, 2)) {
			$unit = 'KB';
			$size = round($size / 1024, 2);
		} else if($size < pow(1024, 3)) {
			$unit = 'MB';
			$size = round($size / pow(1024, 2), 2);
		} else if($size < pow(1024, 4)) {
			$unit = 'GB';
			$size = round($size / pow(1024, 3), 2);
		} else {
			$unit = 'TB';
			$size = round($size / pow(1024, 4), 2);
		}
		return $size.$unit;
	}
	
	public function getSysInfo() {
		$infos = array(
			'服务器环境:' => $this->serverEnv,
			'域名:' => $this->domainName,
			'PHP版本:' => $this->phpVersion,
			'GD库版本:' => $this->gdInfo,
			'FreeType:' => $this->freeType,
			'MYSQL版本:' => $this->mysqlVersion,
			'远程文件获取:' => $this->allowUrlFopen,
			'Curl信息:' => $this->curlInfo,
			'文件上传:' => $this->fileUpload,
			'数据库使用:' => $this->dbSize,
			'脚本最大执行时间' => $this->maxExeTime
		);
		return $infos;
	}
	
	public function index() {
		$sysInfoArr = $this->getSysInfo();
		
		$this->assign('sysInfoArr', $sysInfoArr);
		
		$this->display();
	}
}
